package com.woniu.humanresource.controller;

import com.woniu.humanresource.entity.Course;
import com.woniu.humanresource.entity.PageBean;
import com.woniu.humanresource.entity.ResponseText;
import com.woniu.humanresource.exception.GeneralException;
import com.woniu.humanresource.service.CourseService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@CrossOrigin
@RestController
public class CourseController {

    @Resource
    CourseService courseService;

    // 删除课程
    @DeleteMapping("/course/{cid}")
    public ResponseText deleteCourse(@PathVariable("cid") int cid){
        ResponseText responseText = null;
        try {
            courseService.deleteCourse(cid);
            responseText = new ResponseText(null,200,"删除成功！！！");
        }catch (Exception e){
            e.printStackTrace();
            throw new GeneralException();
        }
        return responseText;
    }

    // 添加课程
    @PostMapping("/course")
    public ResponseText addCourse(@RequestBody Course course){
        ResponseText responseText = null;
        try {
            courseService.insertCourse(course);
            responseText = new ResponseText(null,200,"添加成功！！！");
        }catch (Exception e){
            e.printStackTrace();
            throw new GeneralException();
        }
        return responseText;
    }
    // 修改课程
    @PutMapping("/course")
    public ResponseText updateCourse(@RequestBody Course course){
        ResponseText responseText = null;
        try {
            courseService.updateCourse(course);
            responseText = new ResponseText(null,200,"修改成功！！！");
        }catch (Exception e){
            e.printStackTrace();
            throw new GeneralException();
        }
        return responseText;
    }
    // 查询课程
    @PostMapping("/course/{pageIndex}/{pageSize}")
    public ResponseText selectCourse(@PathVariable("pageIndex") int pageIndex,
                                       @PathVariable("pageSize") int pageSize,
                                       @RequestBody Course course){
        ResponseText<PageBean<Course>> responseText = null;
        try {
            PageBean<Course> pageBean = courseService.selectCourseCondition(course,pageIndex,pageSize);
            responseText = new ResponseText<>(pageBean,200,"查询成功！！！");
        }catch (Exception e){
            e.printStackTrace();
            throw new GeneralException();
        }
        return responseText;
    }

    // 获取全部课程
    @GetMapping("/course")
    public ResponseText getAllCourse(){
        ResponseText responseText = null;
        try {
            List<Course> allCourse = courseService.getAllCourse();
            responseText = new ResponseText(allCourse,200,"获取全部课程成功！！！");
        }catch (Exception e){
            e.printStackTrace();
            throw new GeneralException();
        }
        return responseText;
    }
}
